package controllers

import (
	"fmt"
	"gorm.io/gorm"
	"main.go/database"
	"project/app/model"
	"net/http"
	"strconv"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

//查找
func find_house(db *gorm.DB, request model.house) bool {
	var house model.house
	db.Where("house_id = ?",request.house_id).First(&house)
	if house.house_id != "" {
		fmt.Println("查找成功")
		return true
	}
	fmt.Println("查找失败")
	return false
}

//插入
func insert_house(db *gorm.DB, request model.house) bool {
	if find_house(db, request) == true {
		fmt.Println("该仓库已存在，不能插入")
		return false
	}
	db.Create(&request) //创建一个元组
	fmt.Println("插入成功")
	return true
}

//删除
func delete_house(db *grom.DB, request model.house) bool {
	if find_house(db, request) == false {
		fmt.Println("不存在该仓库，无法删除")
		return false
	}
	db.Where("house_id = ?", request.house_id).Delete(&request)
	fmt.Println("删除成功")
	return true
}

//更新
func update_house(db *grom.DB, request model.house) bool {
	if find_house(db, request) == false {
		fmt.Println("无该仓库，无法更新")
		return false
	}
	db.Model(&request).Where("house_id = ?", request.house_id).Updates(
		model.house{
			house_id : request.house_id
			house_in_tun  : request.house_in_tun
			house_out_tun : request.house_out_tun
			house_stay_time  : request.house_stay_time
			house_odd_tun  : request.house_odd_tun
			house_in_house_time : request.house_in_house_time
			house_out_house_time  : request.house_out_house_time
			house_port_company_name  : request.house_port_company_name
			house_goods_id : request.house_goods_id
		},
	)
	fmt.Println("更新成功")
	return true
}
func web_show_house(c *gin.Context)  {
	db = database.GetDB()
	var house []model.house
	result := db.Table("house").Find(&house)
	if result != nil{
		fmt.Println("!!")
	}
	fmt.Println(house)
	c.JSON(http.StatusOK, gin.H{
		"code" : "200",
		"houes" : house,
		"msg" : "成功",
	})
}
func web_get_house(c *gin.Context){
	db := database.GetDB()
	var house1 model.house
	var house2 model.house
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	house1.house_id = request["house_id"].(string)
	db.Where("house_id = ?", house1.house_id).First(&house2)
	if house2.house_id != "" {
		fmt.Println("成功查找到仓库")
		c.JSON(200, gin.H{
			"code":             "200",
			"purchase_details": house2, //传一个对象
			"msg":              "成功",
		})
	} else {
		c.JSON(404, gin.H{
			"code":             "404",
			"car_id": 			house2, //传一个空对象
			"msg":              "no",
		})
		fmt.Println("未找到该仓库")
	}
}
func web_add_house(c *gin.Context)  {
	db := database.GetDB()
	var house model.house
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	house.house_id = request["house_id"].(string)
	if insert_house(db,house){
		c.JSON(200,gin.H{
			"code" : "200",
			"msg" : "成功",
		})
		fmt.Println("添加成功")
	}else{
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("添加失败")
	}
}
func web_delete_house(c *gin.Context)  {
	db := database.GetDB()
	var house model.house
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	house.house_id = request["house_id"].(string)
	if update_house(db,house){
		c.JSON(200,gin.H{
			"code" : "200",
			"msg" : "成功",
		})
		fmt.Println("更新成功")
	}else{
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("更新失败")
	}
}
func web_update_house(c *gin.Context)  {
	db := database.GetDB()
	var house model.house
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	house.house_id = request["house_id"].(string)
	house.house_in_tun = strconv.Atoi(request["house_in_tun"].(string))
	house.house_out_tun = strconv.Atoi(request["house_out_tun"].(string))
	house.house_stay_time = strconv.Atoi(request["house_stay_time"].(string))
	house.house_odd_tun = strconv.Atoi(request["house_odd_tun"].(string))
	house.house_in_house_time = request["house_in_house_time"].(string)
	house.house_out_house_time = request["house_out_house_time"].(string)
	house.house_port_company_name = request["house_port_company_name"].(string)
	house.house_goods_id = request["house_goods_id"].(string)
	if update_house(db,house){
		c.JSON(200,gin.H{
			"code" : "200",
			"msg" : "成功",
		})
		fmt.Println("添加成功")
	}else{
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("添加失败")
	}
}